from exts import db
from shortuuid import uuid
from enum import Enum
from werkzeug import generate_password_hash,check_password_hash
from datetime import  datetime

class GenderEnum(Enum):
    MALE = 0
    FEMALE = 1
    SECERT = 2
    UNKNOW = 3


class FrontUser(db.Model):
    __tablename__ = "front_users"
    id = db.Column(db.String(100),primary_key=True,default=uuid)
    telephone = db.Column(db.String(100),unique=True,nullable=False)
    _password = db.Column(db.String(100),nullable=False)
    username = db.Column(db.String(100),unique=True,nullable=False)
    email = db.Column(db.String(100))
    realname = db.Column(db.String(50))
    avatar = db.Column(db.String(100))
    signature = db.Column(db.String(100))
    gender = db.Column(db.Enum(GenderEnum),default=GenderEnum.UNKNOW)
    join_time = db.Column(db.DateTime,default=datetime.now)

    def __init__(self,*args,**kwargs):
        if "password" in kwargs:
            self.password = kwargs.get("password")
        # kwargs.pop("password")
            kwargs.pop("password")


        super(FrontUser, self).__init__(*args,**kwargs)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw_password):
        self._password = generate_password_hash(raw_password)

    def check_password(self,raw_password):
        result = check_password_hash(self.password,raw_password)
        return result




